﻿#include "OiPCH.hpp"

#include <QtCharts/QChart>
#include <QtCharts/QValueAxis>

namespace Oi
{
    using namespace PLScannerCore;
    static QFont getFont(int size)
    {
        QFont font;
        font.setPixelSize(size);
        return font;
    }
    namespace PLScannerDll
    {
        class PlyPageImpl : public Impl<PlyPage>
        {
        public:
            void init();
            void updateParams();
            void updateTol(ValueLabel* label, int index, QString key);
            void updateTol(double& upper, double& lower, int index, QString key);
            void setTitalLable(QLabel* pLable, int backColor = 0);

        public:
            QVBoxLayout* _mainLayout = nullptr;

        public:
            ProductParams _params;

        public:
            ToolProfilesView* _profile;
            LineView* _tread;
            ValueLabel* _width;
            ValueLabel* _width1;
            ValueLabel* _thickness1;
            ValueLabel* _thickness2;
            ValueLabel* _thickness3;
            ValueLabel* _thickness4;
            ValueLabel* _thickness5;
            double _widthStd;
            double _widthTol;
            double _thickness1Std;
            double _thickness1Tol;
            double _thickness2Std;
            double _thickness2Tol;
            double _thickness3Std;
            double _thickness3Tol;
            double _thickness4Std;
            double _thickness4Tol;
            double _thickness5Std;
            double _thickness5Tol;

        public:
            QMap<ValueLabel*, double> warningValue;
            QMap<ValueLabel*, double> errorValue;
<<<<<<< Updated upstream
            QList<QLabel*> _lstLable;
=======
            QList<MeasureWidget*> _lstWidget;
>>>>>>> Stashed changes
            QLabel* _disT1T4;
            double _errorThicknessRange;
            double _errorWidthRange;
            double _edgeThickness;
            int    _precision;

        };

        void PlyPageImpl::init()
        {
            OI_Q(PlyPage);
<<<<<<< Updated upstream
=======
            _measureNum = Core::OiSettings()->value("View/MeasureNum", 10).toInt();

            QHBoxLayout* mainLayout1 = new QHBoxLayout();
>>>>>>> Stashed changes
            
            QList<QLabel*> titleLable;
            for (int i = 0; i < 4 ;i++)
            {
                if (i == 0)
                {
                    QLabel* totallable = new QLabel("总宽");
                    QLabel* totalELlable = new QLabel("总宽EL");
                    QLabel* t1lable = new QLabel("操作侧边厚T1");
                    QLabel* t2lable = new QLabel("操作侧中厚T2");
                    QLabel* t3lable = new QLabel("驱动侧中厚T3");
                    QLabel* t4lable = new QLabel("驱动侧边厚T4");

                    QLabel* standardLable = new QLabel("标准值");
                    QLabel* measureLable = new QLabel("测量值");
                    QLabel* disLable = new QLabel("差异值");
                    setTitalLable(totallable);
                    setTitalLable(totalELlable);
                    setTitalLable(t1lable);
                    setTitalLable(t2lable);
                    setTitalLable(t3lable);
                    setTitalLable(t4lable);
                    _lstLable.append(totallable);
                    _lstLable.append(totalELlable);
                    _lstLable.append(t1lable);
                    _lstLable.append(t2lable);
                    _lstLable.append(t3lable);
                    _lstLable.append(t4lable);
                    setTitalLable(standardLable);
                    setTitalLable(measureLable);
                    setTitalLable(disLable);
                    titleLable.append(standardLable);
                    titleLable.append(measureLable);
                    titleLable.append(disLable);
                    continue;
                }
                else
                {
                    _lstLable.append(titleLable[i - 1]);
                }
                for (int j = 0; j < 7 ; j++)
                {
                    QLabel* lable = new QLabel();
                    setTitalLable(lable,i);
                    
                    if (j != 0)
                    {
                        _lstLable.append(lable);
                    }
                    
                }
            }
<<<<<<< Updated upstream
=======
            for (int index = 0; index < _measureNum; index++)
            {
                QString strName = QString("测量点T%1").arg(index+1);
                MeasureWidget* pWidget = new MeasureWidget(q);
                pWidget->init(strName);
                _lstWidget.append(pWidget);

                mainLayout1->addWidget(pWidget->_pLableWidget);
            }

>>>>>>> Stashed changes
            QVBoxLayout* mainLayout = new QVBoxLayout(q);

            _profile = new ToolProfilesView();

            _profile->setPageFlag(PMP_ThicknessMeasure);

            _tread = new LineView();

            _profile->setParams(_params);

<<<<<<< Updated upstream
            QGridLayout* botLayout = new QGridLayout();

            _width = new ValueLabel();
            _width->setText(PlyPage::tr("总宽"));
            _width->setPrecise(2);
            _width->setUnit("mm");
            _width->setCheckEnable(true);

            _width1 = new ValueLabel();
            _width1->setText(PlyPage::tr("总宽EL"));
            _width1->setPrecise(1);
            _width1->setUnit("mm");
            _width1->setCheckEnable(true);

            _thickness1 = new ValueLabel();
            _thickness1->setText(PlyPage::tr("边厚T1"));
            _thickness1->setPrecise(2);
            _thickness1->setUnit("mm");
            _thickness1->setCheckEnable(true);

            _thickness2 = new ValueLabel();
            _thickness2->setText(PlyPage::tr("中厚T2"));
            _thickness2->setPrecise(2);
            _thickness2->setUnit("mm");
            _thickness2->setCheckEnable(true);

            _thickness3 = new ValueLabel();
            _thickness3->setText(PlyPage::tr("中厚T3"));
            _thickness3->setPrecise(2);
            _thickness3->setUnit("mm");
            _thickness3->setCheckEnable(true);

            _thickness4 = new ValueLabel();
            _thickness4->setText(PlyPage::tr("边厚T4"));
            _thickness4->setPrecise(2);
            _thickness4->setUnit("mm");
            _thickness4->setCheckEnable(true);

            /*           _thickness5 = new ValueLabel();
                       _thickness5->setText(PlyPage::tr("厚度5"));
                       _thickness5->setPrecise(2);
                       _thickness5->setUnit("mm");
                       _thickness5->setCheckEnable(true);*/

            updateTol(_width, TT_TotolWidth, "x");
             updateTol(_width1, TT_TotolWidth, "x");
             updateTol(_thickness1, TT_MeasureThicknessTool + 0, "y");
             updateTol(_thickness2, TT_MeasureThicknessTool + 1, "y");
             updateTol(_thickness3, TT_MeasureThicknessTool + 2, "y");
             updateTol(_thickness4, TT_MeasureThicknessTool + 3, "y");
//             updateTol(_thickness5, TT_MeasureThicknessTool + 4, "y");

             updateTol(_widthStd, _widthTol, TT_TotolWidth, "x");
             updateTol(_thickness1Std, _thickness1Tol, TT_MeasureThicknessTool + 0, "y");
             updateTol(_thickness2Std, _thickness2Tol, TT_MeasureThicknessTool + 1, "y");
             updateTol(_thickness3Std, _thickness3Tol, TT_MeasureThicknessTool + 2, "y");
             updateTol(_thickness4Std, _thickness4Tol, TT_MeasureThicknessTool + 3, "y");
//             updateTol(_thickness5Std, _thickness5Tol, TT_MeasureThicknessTool + 4, "y");
             /*
             botLayout->addWidget(_width, 0, 1);
             botLayout->addWidget(_width1, 0, 2);
             botLayout->addWidget(_thickness1, 0, 3);
             botLayout->addWidget(_thickness2, 0, 4);
             botLayout->addWidget(_thickness3, 0, 5);
             botLayout->addWidget(_thickness4, 0, 6);
             botLayout->addWidget(_thickness5, 0, 7);*/

            int index = 0;
            for (int row = 0; row < 4; row++)
            {
                for (int col = 0; col < 7; col++)
                {
                    if (row == 0 && col == 0)
                    {
                        continue;
                    }
                    botLayout->addWidget(_lstLable[index], row, col);
                    index++;
                }
            }
            QLabel* disLable1 = new QLabel("边厚差异");
            setTitalLable(disLable1, 0);
            _disT1T4 = new QLabel("");
            setTitalLable(_disT1T4, 3);
            botLayout->addWidget(disLable1, 1, 8);
            botLayout->addWidget(_disT1T4, 2, 8);
            botLayout->setSpacing(2);

=======
>>>>>>> Stashed changes
            mainLayout->addWidget(_profile, 2);
            mainLayout->addWidget(_tread, 1);
            mainLayout->addLayout(botLayout, 1);

            _tread->setTol(0.05, 0.03);

            _profile->winId();

            QMetaObject::invokeMethod(q, "onReady");
        }
        void PlyPageImpl::setTitalLable(QLabel* pLable,int backColor)
        {
            pLable->setFont(getFont(27));
            pLable->setFixedSize(200, 35);
            pLable->setAlignment(Qt::AlignCenter);
            if (backColor == 0)
            {
                pLable->setStyleSheet("QLabel{color: black;border-style: groove;border-radius:5px;}");
            }
            else if (backColor == 1)
            {
                pLable->setStyleSheet("QLabel{color: black;background-color: white;border-style: groove;border-radius:5px;}");
            }
            else if(backColor == 2)
            {
                pLable->setStyleSheet("QLabel{color: black;background-color: green;border-style: groove;border-radius:5px;}");
            }
            else if (backColor == 3)
            {
                pLable->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240);border-style: groove;border-radius:5px;}");
            }
            pLable->setFrameShape(QFrame::Box);
            pLable->setFrameShadow(QFrame::Raised);
        }
        void PlyPageImpl::updateParams()
        {
            OI_Q(PlyPage);

            _profile->setParams(_params);
<<<<<<< Updated upstream
            if (PLScanner::instance()->widthHide == 1)
            {
                for (int i = 0; i< 24;i+=7)
                {
                    _lstLable[i]->setHidden(TRUE);
                }
            }
            if (PLScanner::instance()->widthHide == 2)
            {
                for (int i = 1; i < 24; i += 7)
                {
                    _lstLable[i]->setHidden(TRUE);
                }
            }
            //_width->setVisible(PLScanner::instance()->widthHide != 1);
            //_width1->setVisible(PLScanner::instance()->widthHide != 2);

            updateTol(_width, TT_TotolWidth, "x");
            updateTol(_width1, TT_TotolWidth, "x");
            updateTol(_thickness1, TT_MeasureThicknessTool + 0, "y");
            updateTol(_thickness2, TT_MeasureThicknessTool + 1, "y");
            updateTol(_thickness3, TT_MeasureThicknessTool + 2, "y");
            updateTol(_thickness4, TT_MeasureThicknessTool + 3, "y");
/*            updateTol(_thickness5, TT_MeasureThicknessTool + 4, "y");*/

            updateTol(_widthStd, _widthTol, TT_TotolWidth, "x");
            updateTol(_thickness1Std, _thickness1Tol, TT_MeasureThicknessTool + 0, "y");
            updateTol(_thickness2Std, _thickness2Tol, TT_MeasureThicknessTool + 1, "y");
            updateTol(_thickness3Std, _thickness3Tol, TT_MeasureThicknessTool + 2, "y");
            updateTol(_thickness4Std, _thickness4Tol, TT_MeasureThicknessTool + 3, "y");
/*            updateTol(_thickness5Std, _thickness5Tol, TT_MeasureThicknessTool + 4, "y");*/
           
=======


            _lstWidget[1]->_pLableWidget->setVisible(!PLScanner::instance()->widthHide == 1);
            _lstWidget[2]->_pLableWidget->setVisible(PLScanner::instance()->widthHide == 2);
           
            if (_lstWidget.size() > 2)
            {
                _lstWidget[1]->setValue(_params.materialsWidth,1);
                _lstWidget[2]->setValue(_params.materialsWidth, 1);
            }

            int count = min(_params.measureThickness.size(), _lstWidget.size());
            for (int index = 0; index < count; index++)
            {
                if (index + 3 < _lstWidget.size())
                {
                    updateMeasureWidget(_lstWidget[index + 3], TT_MeasureThicknessTool + index, "y");
                }
            }

>>>>>>> Stashed changes
            OiProfile pfs;

            pfs.push_back(QPointF( -_params.materialsWidth / 2, 0));
            pfs.push_back(QPointF(-_params.materialsWidth / 4, +0.1));
            pfs.push_back(QPointF(+_params.materialsWidth / 4, -0.1));
            pfs.push_back(QPointF(+_params.materialsWidth / 2, 0));
            _params.getMeasureErrorTol(TT_TotolWidth, "x", _errorWidthRange);
            _params.getMeasureErrorTol(TT_MeasureThicknessTool, "y", _errorThicknessRange);

            
            _tread->display(pfs);
            _tread->zoomFit();
        }
<<<<<<< Updated upstream
=======
        void PlyPageImpl::updateMeasureWidget(MeasureWidget* label, int index, QString key)
        {
            if (!label|| label == nullptr)
            {
                return;
            }
            label->setEnabled(true);
            int enable = 0;
            double warningTol = 0.0;
            double errorTol = 0.0;
            double stdValue = 0.0;
            label->_difValue = 0;
            label->_errorValue = 0;
            label->_maxErrorValue = 0;
            label->_maxWarningValue = 0;
            label->_minErrorValue = 0;
            label->_measureValue = 0;
            label->_minWarningValue = 0;
            label->_standardValue = 0;
            label->_warningValue = 0;
            if (_params.getMeasureEnable(index , "y", enable) &&
                _params.getMeasureWarningTol(index , "y", warningTol) &&
                _params.getMeasureErrorTol(index , "y", errorTol) &&
                _params.getStdValue(index , "y", stdValue))
            {
                if (enable & TC_WarningEnable)
                {
                    label->setEarlyUpper(stdValue + warningTol);
                    label->setEarlyLower(stdValue - warningTol);
                    //                        warningValue[label] = warningTol;
                }
>>>>>>> Stashed changes

        void PlyPageImpl::updateTol(ValueLabel* label, int index, QString key)
        {
            int enable = 0;
            double warningTol = 0.0;
            double errorTol = 0.0;
            double stdValue = 0.0;

            if (_params.getMeasureEnable(index, key, enable) &&
                _params.getMeasureWarningTol(index, key, warningTol) &&
                _params.getMeasureErrorTol(index, key, errorTol) &&
                _params.getStdValue(index, key, stdValue))
            {
                label->setEnabled(true);
                if (enable & TC_WarningEnable)
                {
                    label->setEarlyUpper(stdValue + warningTol);
                    label->setEarlyLower(stdValue - warningTol);
                    warningValue[label] = warningTol;
                }

                if (enable & TC_ErrorEnable)
                {
                    label->setUpper(stdValue + errorTol);
                    label->setLower(stdValue - errorTol);
                    errorValue[label] = errorTol;
                }

                label->setStdValue(stdValue);
            }
            else
            {
                label->setEnabled(false);
            }
        }

        void PlyPageImpl::updateTol(double& std, double& err, int index, QString key)
        {
            double errorTol = 0.0;
            double stdValue = 0.0;

            if (_params.getMeasureErrorTol(index, key, errorTol) &&
                _params.getStdValue(index, key, stdValue))
            {
                err = errorTol;
                std = stdValue;
            }
            else
            {
                std = 0;
                err = 0;
            }
        }

        PlyPage::PlyPage(QWidget * parent /*= nullptr*/)
            : PageAbstract(parent)
        {
            OI_I(PlyPage)->init();
        }

        void PlyPage::setParams(const ProductParams& params)
        {
            OI_F(PlyPage);
            d->_params = params;
            d->updateParams();
        }

        int PlyPage::setResult(const PLScannerCore::WorkResult::Result& result)
        {
            OI_F(PlyPage);
            int resultCode = result.result;
   
            d->_params.getMeasureEdgeThickness(d->_edgeThickness);
            d->_params.getMeasurePrecision(d->_precision);
<<<<<<< Updated upstream
            d->_profile->setResult(result);

=======
            PLScannerCore::WorkResult::Result resultTemp = result;

            DataRow row;
            for (int i = 0; i < 10; i++)
            {
                row.lstThickness.append(0);
                row.lstThicknessErr.append(0);
                row.lstThicknessStd.append(0);
                row.lstThicknessDif.append(0);
            }
>>>>>>> Stashed changes
            double v1 = 0.0, v2 = 0.0, v3 = 0.0, v4 = 0.0, v5 = 0.0, v6 = 0.0, v7 = 0.0 ,vdis = 0.0;

            auto rdata = result.data;
            //for (auto iter = rdata.constBegin(); iter != rdata.constEnd();iter++)
            //{
            //    OiWarning() << iter.key();
            //    OiWarning() << iter.value();
            //}
            if (rdata.contains("diffValue"))
            {
                OiProfile diff = rdata["diffValue"].value<OiProfile>();
                d->_tread->display(diff);
                d->_tread->zoomFit();
            }

            if (rdata.contains("tolWidth"))
            {
                double tolWidth = rdata["tolWidth"].toDouble();
                
                d->_width->setValue(v1);
                QString strValue = QString::number(tolWidth, 'f', d->_precision);
                d->_lstLable[14]->setText(strValue);
                v1 = strValue.toDouble();
            }

            if (rdata.contains("tolWidth2"))
            {
                double tolWidth2 = rdata["tolWidth2"].toDouble();
                if (tolWidth2 < 0.00001)
                {
                    tolWidth2 = 0;
                }
              
                d->_width1->setValue(v7);
                QString strValue = QString::number(tolWidth2, 'f', d->_precision);
                d->_lstLable[15]->setText(strValue);
                v7 = strValue.toDouble();
            }

            if (rdata.contains("measureThickness"))
            {
                QList<QPointF> values = rdata["measureThickness"].value<QList<QPointF>>();
<<<<<<< Updated upstream

                if (values.size() > 0)
                {
                    v2 = values[0].y();
                    v2 = QString::number(v2, 'f', d->_precision).toDouble();
                }
                if (values.size() > 1)
                {
                    v3 = values[1].y();
                    v3 = QString::number(v3, 'f', d->_precision).toDouble();
                }
                if (values.size() > 2)
                {
                    v4 = values[2].y();
                    v4 = QString::number(v4, 'f', d->_precision).toDouble();
 //                   d->_thickness3->setValue(v4);
                }
                if (values.size() > 3)
                {
                    v5 = values[3].y();
                    v5 = QString::number(v5, 'f', d->_precision).toDouble();
 //                   d->_thickness4->setValue(v5);
                }
                if (values.size() > 4)
                {
                    v6 = values[4].y();
                    v6 = QString::number(v6, 'f', d->_precision).toDouble();
//                    d->_thickness5->setValue(v6);
                }
                for (int i = 0; i < values.size();i++)
                {
                    vdis = values[i].y();
                    QString strValue = QString::number(vdis, 'f', d->_precision);
                    d->_lstLable[i+16]->setText(strValue);
                }
                
            }
            OiWarning() <<"v1:" <<v1 << "v2:" << v2 << "v3:" << v3 << "v4:" << v4 << "v5:" << v5 << "v6:" << v6;
            int resultCode = result.result;

            if (resultCode == WorkResult::RC_Success)
            {
                int a = d->_thickness3->status();
                if (/*(PLScanner::instance()->widthHide != 1 && d->_width->status() == ValueLabel::LS_Error) ||*/
                    d->_thickness1->status() == ValueLabel::LS_Error ||
                    d->_thickness2->status() == ValueLabel::LS_Error ||
                    d->_thickness3->status() == ValueLabel::LS_Error ||
                    d->_thickness4->status() == ValueLabel::LS_Error 
                    /*d->_thickness5->status() == ValueLabel::LS_Error*/ /*||
                    d->_width1->status() == ValueLabel::LS_Error*/)
                {
//                    resultCode = WorkResult::RC_Error;
                }
                else if (/*(PLScanner::instance()->widthHide != 1 && d->_width->status() == ValueLabel::LS_Warning) ||*/
                    d->_thickness1->status() == ValueLabel::LS_Warning ||
                    d->_thickness2->status() == ValueLabel::LS_Warning ||
                    d->_thickness3->status() == ValueLabel::LS_Warning ||
                    d->_thickness4->status() == ValueLabel::LS_Warning 
                   /* d->_thickness5->status() == ValueLabel::LS_Warning*//*  ||
                   d->_width1->status() == ValueLabel::LS_Warning*/)
                {
 //                   resultCode = WorkResult::RC_Warning;
                }
            }

           

            QString strValue = QString::number(d->_widthStd, 'f', d->_precision);
            d->_lstLable[7]->setText(strValue);
            d->_lstLable[7]->setStyleSheet("QLabel{color: black;background-color: white;}");
            if (qAbs(v1 - d->_widthStd) > d->_widthTol)
            {
                d->_lstLable[14]->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                d->_lstLable[21]->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");
            }
            else
            {
                d->_lstLable[14]->setStyleSheet("QLabel{color: black;background-color: green;}");
                d->_lstLable[21]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
            }
            strValue = QString::number(d->_widthStd, 'f', d->_precision);
            d->_lstLable[8]->setText(strValue);
            d->_lstLable[8]->setStyleSheet("QLabel{color: black;background-color: white;}");
            if (qAbs(v7 - d->_widthStd) > d->_widthTol)
            {
                d->_lstLable[15]->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                d->_lstLable[22]->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");
            }
            else
            {
                d->_lstLable[15]->setStyleSheet("QLabel{color: black;background-color: green;}");
                d->_lstLable[22]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
            }
            strValue = QString::number(d->_thickness1Std, 'f', d->_precision);
            d->_lstLable[9]->setText(strValue);
            d->_lstLable[9]->setStyleSheet("QLabel{color: black;background-color: white;}");
            if (qAbs(v2 - d->_thickness1Std) > d->_thickness1Tol)
            {
                d->_lstLable[16]->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                d->_lstLable[23]->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Error;
            }
            else  if (qAbs(v2 - d->_thickness1Std) > d->warningValue[d->_thickness1])
            {
                d->_lstLable[16]->setStyleSheet("QLabel{color: black;background-color: rgb(255, 255, 0);}");
                d->_lstLable[23]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Warning;
            }
            else
            {
                d->_lstLable[16]->setStyleSheet("QLabel{color: black;background-color: green;}");
                d->_lstLable[23]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
            }
            strValue = QString::number(d->_thickness2Std, 'f', d->_precision);
            d->_lstLable[10]->setText(strValue);
            d->_lstLable[10]->setStyleSheet("QLabel{color: black;background-color: white;}");
            if (qAbs(v3 - d->_thickness2Std) > d->_thickness2Tol)
            {
                d->_lstLable[17]->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                d->_lstLable[24]->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Error;
            }
            else  if (qAbs(v3 - d->_thickness2Std) > d->warningValue[d->_thickness2])
            {
                d->_lstLable[17]->setStyleSheet("QLabel{color: black;background-color: rgb(255, 255, 0);}");
                d->_lstLable[24]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Warning;
            }
            else
            {
                d->_lstLable[17]->setStyleSheet("QLabel{color: black;background-color: green;}");
                d->_lstLable[24]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
            }
            strValue = QString::number(d->_thickness3Std, 'f', d->_precision);
            d->_lstLable[11]->setText(strValue);
            d->_lstLable[11]->setStyleSheet("QLabel{color: black;background-color: white;}");
            if (qAbs(v4 - d->_thickness3Std) > d->_thickness3Tol)
            {
                d->_lstLable[18]->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                d->_lstLable[25]->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Error;
            }
            else  if (qAbs(v4 - d->_thickness3Std) > d->warningValue[d->_thickness3])
            {
                d->_lstLable[18]->setStyleSheet("QLabel{color: black;background-color: rgb(255, 255, 0);}");
                d->_lstLable[25]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Warning;
            }
            else
            {
                d->_lstLable[18]->setStyleSheet("QLabel{color: black;background-color: green;}");
                d->_lstLable[25]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
            }
            strValue = QString::number(d->_thickness4Std, 'f', d->_precision);
            d->_lstLable[12]->setText(strValue);
            d->_lstLable[12]->setStyleSheet("QLabel{color: black;background-color: white;}");
            if (qAbs(v5 - d->_thickness4Std) > d->_thickness4Tol)
            {
                d->_lstLable[19]->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                d->_lstLable[26]->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");

                resultCode = WorkResult::RC_Error;
            }
            else  if (qAbs(v5 - d->_thickness4Std) > d->warningValue[d->_thickness4])
            {
                d->_lstLable[19]->setStyleSheet("QLabel{color: black;background-color: rgb(255, 255, 0);}");
                d->_lstLable[26]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Warning;
            }
            else
            {
                d->_lstLable[19]->setStyleSheet("QLabel{color: black;background-color: green;}");
                d->_lstLable[26]->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
            }
           


            strValue = QString::number(v1-d->_widthStd, 'f', d->_precision);
            d->_lstLable[21]->setText(strValue);            
            strValue = QString::number(v7-d->_widthStd, 'f', d->_precision);
            d->_lstLable[22]->setText(strValue);
            strValue = QString::number(v2-d->_thickness1Std, 'f', d->_precision);
            d->_lstLable[23]->setText(strValue);
            resultTemp.data["ThicknessDis1"] = QVariant::fromValue(strValue);
            strValue = QString::number(v3-d->_thickness2Std, 'f', d->_precision);
            d->_lstLable[24]->setText(strValue);
            resultTemp.data["ThicknessDis2"] = QVariant::fromValue(strValue);
            strValue = QString::number(v4-d->_thickness3Std, 'f', d->_precision);
            d->_lstLable[25]->setText(strValue);
            resultTemp.data["ThicknessDis3"] = QVariant::fromValue(strValue);
            strValue = QString::number(v5-d->_thickness4Std, 'f', d->_precision);
            d->_lstLable[26]->setText(strValue);
            resultTemp.data["ThicknessDis4"] = QVariant::fromValue(strValue);
            strValue = QString::number(v2 - v5, 'f', d->_precision);
            d->_disT1T4->setText(strValue);
            resultTemp.data["ThicknessDis"] = QVariant::fromValue(strValue);
            if (qAbs(v2 - v5) > d->_edgeThickness)
            {
                d->_disT1T4->setStyleSheet("QLabel{color: red;background-color: rgb(102, 204, 240)}");
                resultCode = WorkResult::RC_Error;
            }
            else
            {
                d->_disT1T4->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240);}");
            }

            DataRow row;
=======
                for (int index = 0; index < values.size(); index++)
                {
                    double val = values[index].y();
                    QString strValue = QString::number(val, 'f', d->_precision);
                    auto compensation = PLScanner::instance()->compensation;
                    val = strValue.toDouble() + compensation;
                    if (resultCode < d->_lstWidget[index + 3]->setValue(val, 2))
                    {
                        resultCode = d->_lstWidget[index + 3]->setValue(val, 2);
                    }
                    ;
                    if (index < 10)
                    {
                        row.lstThickness[index] = val ;
                        row.lstThicknessStd[index] = d->_lstWidget[index + 3]->_standardValue;
                        row.lstThicknessErr[index] = d->_lstWidget[index + 3]->_errorValue;
                        row.lstThicknessDif[index] = d->_lstWidget[index + 3]->_difValue;
                    }                  
                }
                for (int i = values.size() + 3; i < d->_lstWidget.size(); i++)
                {
                    if (d->_lstWidget[i]->_pLabStandard && d->_lstWidget[i]->_pLabMeasure && d->_lstWidget[i]->_pLabDisValue)
                    {
                        d->_lstWidget[i ]->_pLabStandard->setText("");
                        d->_lstWidget[i]->_pLabMeasure->setText("");
                        d->_lstWidget[i ]->_pLabDisValue->setText("");
                    }     
                }
            }
     
>>>>>>> Stashed changes
            row.result = resultCode;
            row.datetime = QDateTime::currentDateTime();
            row.formula = d->_params.specName;
            row.width = v1;
            row.width1 = v7;
            row.user = UserManage::instance()->current();
            row.team = UserManage::instance()->team();
            row.thickness1 = v2;
            row.thickness2 = v3;
            row.thickness3 = v4;
            row.thickness4 = v5;
            row.widthStd = d->_widthStd;
            row.widthErr = d->_widthTol;
<<<<<<< Updated upstream
            row.thickness1Std = d->_thickness1Std;
            row.thickness1Err = d->_thickness1Tol;
            row.thickness2Std = d->_thickness2Std;
            row.thickness2Err = d->_thickness2Tol;
            row.thickness3Std = d->_thickness3Std;
            row.thickness3Err = d->_thickness3Tol;
            row.thickness4Std = d->_thickness4Std;
            row.thickness4Err = d->_thickness4Tol;
            row.thickness1Dif = v2 - d->_thickness1Std;
            row.thickness2Dif = v3 - d->_thickness2Std;
            row.thickness3Dif = v4 - d->_thickness3Std;
            row.thickness4Dif = v5 - d->_thickness4Std;
            row.edgeThicknessDif = v2 - v5;
=======
            d->_profile->setResult(result);
>>>>>>> Stashed changes
            row.profile = result.profile;
           
            Database::instance()->insert(row);

            if (resultCode == WorkResult::RC_Success)
            {
                Motor::instance()->setResult(1);
            }
            else if (resultCode == WorkResult::RC_Warning)
            {
                Motor::instance()->setResult(0);
            }
            else
            {
                Motor::instance()->setResult(2);
            }

            auto outputSystem = Core::OiSettings()->value("System/OutputSystem").toString();
            
            if (outputSystem == "Linglong_Local_DB")
            {
                if (PLScannerOutput::saveToSys(row, outputSystem))
                {

                }
            }
            else if (outputSystem == "OPC")
            {
                if (PLScannerOutput::saveToSys(resultTemp, outputSystem))
                {

                }
            }
                

            return resultCode;
        }

        void PlyPage::onReady()
        {
            OI_F(PlyPage);
            d->_profile->zoomFit();
            d->_tread->zoomFit();
        }

        void PlyPage::resizeEvent(QResizeEvent *event)
        {
            OI_F(PlyPage);
            d->_profile->zoomFit();
            d->_tread->zoomFit();
        }

        void PlyPage::setRun(bool flag)
        {
            OI_F(PlyPage);
        }

        void PlyPage::toCurrent()
        {
            OI_F(PlyPage);
        }


<<<<<<< Updated upstream
=======
        MeasureWidget::~MeasureWidget()
        {

        }

        QWidget* MeasureWidget::init(QString name)
        {

           _FontSize = Core::OiSettings()->value("View/WidgetFontSize").toInt();
           _Width = Core::OiSettings()->value("View/FixedWidth").toInt();
           _Hight = Core::OiSettings()->value("View/FixedHight").toInt();


            _pLabTitle = new QLabel();
            _pLabStandard = new QLabel();
            _pLabMeasure = new QLabel();
            _pLabDisValue = new QLabel();
            QVBoxLayout* pLay = new QVBoxLayout(_pLableWidget);
            setTitalLable(_pLabTitle,0);
            setTitalLable(_pLabStandard, 1);
            setTitalLable(_pLabMeasure, 2);
            setTitalLable(_pLabDisValue, 3);
            pLay->addWidget(_pLabTitle);
            pLay->addWidget(_pLabStandard);
            pLay->addWidget(_pLabMeasure);
            pLay->addWidget(_pLabDisValue);

            _pLabTitle->setText(name);
            //_pLabStandard->setText(name);
            //_pLabMeasure->setText(name);
            //_pLabDisValue->setText(name);
            return _pLableWidget;
        }

        void MeasureWidget::setTitalLable(QLabel* pLable, int backColor)
        {
            pLable->setFont(getFont(_FontSize));
            pLable->setFixedSize(_Width, _Hight);
            pLable->setAlignment(Qt::AlignCenter);
            if (backColor == 0)
            {
                pLable->setStyleSheet("QLabel{color: black;border-style: groove;border-radius:5px;}");
            }
            else if (backColor == 1)
            {
                pLable->setStyleSheet("QLabel{color: black;background-color: white;border-style: groove;border-radius:5px;}");
            }
            else if (backColor == 2)
            {
                pLable->setStyleSheet("QLabel{color: black;background-color: green;border-style: groove;border-radius:5px;}");
            }
            else if (backColor == 3)
            {
                pLable->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240);border-style: groove;border-radius:5px;}");
            }
            pLable->setFrameShape(QFrame::Box);
            pLable->setFrameShadow(QFrame::Raised);
        }

        int MeasureWidget::setValue(double value, int type)
        {
            QString strValue = QString::number(value);
            switch (type)
            {
            case 1:
                _pLabStandard->setText(strValue);
                _standardValue = value;
                break;
            case 2:
                _measureValue = value;
                _pLabMeasure->setText(strValue);
                _difValue = value - _standardValue;
                strValue = QString::number(_difValue);
                _pLabDisValue->setText(strValue);
                _pLabMeasure->setStyleSheet("QLabel{color: black;background-color: green;border-style: groove;border-radius:5px;}");
                if (value >= _maxErrorValue || value <= _minErrorValue)
                {
                    _pLabDisValue->setStyleSheet("QLabel{color: black;background-color: rgb(238, 60, 60)}");
                    return 300;
                }
                else if (value >= _maxWarningValue || value <= _minWarningValue)
                {
                    _pLabDisValue->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240)}");
                    return 200;
                }
                else
                {
                    _pLabDisValue->setStyleSheet("QLabel{color: black;background-color: rgb(102, 204, 240);border-style: groove;border-radius:5px;}");
                }
                break;
            default:
                break;
            }
            return 1;
        }
        void MeasureWidget::setEarlyUpper(double value)
        {
            _maxWarningValue = value;
            _warningValue = _maxWarningValue - _standardValue;
        }
        void MeasureWidget::setEarlyLower(double value)
        {
            _minWarningValue = value;
            _warningValue = _standardValue - _maxWarningValue;
        }
        void MeasureWidget::setUpper(double value)
        {
            _maxErrorValue = value;
            _errorValue = _maxErrorValue - _standardValue;

        }
        void MeasureWidget::setLower(double value)
        {
            _minErrorValue = value;
            _errorValue = _standardValue - _maxErrorValue;
        }
>>>>>>> Stashed changes
    }
}